package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.logic.bussiness.player.UserMineWarBussiness;
import com.yanqu.road.server.pb.MineWarPb;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.minewar.UserMineData;
import com.yanqu.road.entity.minewar.UserMinePit;
import com.yanqu.road.entity.minewar.UserMineRefuge;
import com.yanqu.road.entity.player.UserMineWarPatronsData;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.minewar.MineWarProto;
import com.yanqu.road.server.cmd.minewar.task.PatronsAbilityChangeTask;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.config.MineWarConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.MineWarMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MineWarModule extends GeneralModule {

    private UserMineData userMineData;

    private Map<Integer, Map<Integer, UserMineRefuge>> userMineRefugeMap;

    private Map<Integer, UserMineWarPatronsData> mineWarPatronsDataMap;

    public MineWarModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean afterLoadData() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.MineWar.getValue())){
            initUserMineData(player.getUserId());
            checkMineRefugeInit();
        }
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != userMineData) {
            if (userMineData.isInsertOption()) {
                UserMineWarBussiness.addUserMineData(userMineData);
            } else if (userMineData.isUpdateOption()) {
                UserMineWarBussiness.updateUserMineData(userMineData);
            }
        }
        for(Map<Integer, UserMineRefuge> userRefugeMap : userMineRefugeMap.values()) {
            List<UserMineRefuge> refugeList = new ArrayList<>(userRefugeMap.values());
            for (UserMineRefuge userMineRefuge : refugeList) {
                if (userMineRefuge.isInsertOption()) {
                    UserMineWarBussiness.addUserMineRefuge(userMineRefuge);
                } else if (userMineRefuge.isUpdateOption()) {
                    UserMineWarBussiness.updateUserMineRefuge(userMineRefuge);
                }
            }
        }
        List<UserMineWarPatronsData> tempList = new ArrayList<>(mineWarPatronsDataMap.values());
        for(UserMineWarPatronsData userMineWarPatronsData : tempList){
            if(userMineWarPatronsData.isInsertOption()){
                UserMineWarBussiness.addUserMineWarPatronsData(userMineWarPatronsData);
            }else if(userMineWarPatronsData.isUpdateOption()){
                UserMineWarBussiness.updateUserMineWarPatronsData(userMineWarPatronsData);
            }
        }
        return true;
    }

    @Override
    public boolean loadData() {
        userMineData = UserMineWarBussiness.getUserMineData(player.getUserId());
        MineWarMgr.setUserMineData(userMineData);
        userMineRefugeMap = UserMineWarBussiness.getUserMineRefugeMap(player.getUserId());
        mineWarPatronsDataMap = UserMineWarBussiness.getUserMineWarPatronsDataMap(player.getUserId());
        checkMineWarPatronsMap();
        return true;
    }

    @Override
    public void loginSendMsg() {
        if(null != userMineRefugeMap){
            List<UserMineRefuge> refugeList = new ArrayList<>();
            for(Map<Integer, UserMineRefuge> refugeMap : userMineRefugeMap.values()){
                refugeList.addAll(refugeMap.values());
            }
            syncMineRefuge(refugeList);
        }
        checkMineWarPatronsMap();
        checkPatronsDispatch();
        if(null != mineWarPatronsDataMap && mineWarPatronsDataMap.size() > 0){
            syncUserMineWarPatronsData(new ArrayList<>(mineWarPatronsDataMap.values()));
        }
        List<UserMinePit> pitList = MineWarMgr.getUserMinePitList(player.getUserId());
        syncMinePit(pitList, false, true);
    }

    public void initMineWarSystem(){
        initUserMineData(player.getUserId());
        checkMineRefugeInit();
        checkMineWarPatronsMap();
        if(null != mineWarPatronsDataMap){
            syncUserMineWarPatronsData(new ArrayList<>(mineWarPatronsDataMap.values()));
        }
    }

    private void checkMineWarPatronsMap(){
        if(SystemOpenMgr.systemOpen(player, eSystemId.MineWar.getValue())){
            Map<Integer, UserPatrons> patronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
            for(UserPatrons userPatrons : patronsMap.values()){
                if(!mineWarPatronsDataMap.containsKey(userPatrons.getPatronsId())){
                    initUserMineWarPatronsData(userPatrons.getPatronsId());
                }
            }
        }
    }

    private void checkPatronsDispatch() {
        for(UserMineWarPatronsData patronsData : mineWarPatronsDataMap.values()){
            if(patronsData.getDispatchMine() == MineWarConfigMgr.DISPATCH_MINE_PUBLIC){
                UserMinePit userMinePit = MineWarMgr.getUserMinePit(patronsData.getType(), patronsData.getPitId());
                if(userMinePit.getUserId() != player.getUserId()){
                    patronsData.setType(0);
                    patronsData.setPitId(0);
                    patronsData.setDispatchMine(0);
                }
            }else if(patronsData.getDispatchMine() == MineWarConfigMgr.DISPATCH_MINE_NONE){
                if(patronsData.getType() != 0){
                    patronsData.setType(0);
                }
                if(patronsData.getPitId() != 0){
                    patronsData.setPitId(0);
                }
            }else if(patronsData.getDispatchMine() == MineWarConfigMgr.DISPATCH_MINE_SELF){
                UserMineRefuge userMineRefuge = getUserMineRefuge(patronsData.getType(), patronsData.getPitId());
                if(null == userMineRefuge){
                    patronsData.setDispatchMine(0);
                }
            }
        }
    }

    public void syncMinePit(UserMinePit userMinePit, boolean isRemove){
        if(null != userMinePit){
            List<UserMinePit> syncList = new ArrayList<>();
            syncList.add(userMinePit);
            syncMinePit(syncList, isRemove, false);
        }
    }

    private void syncMinePit(List<UserMinePit> userMinePitList, boolean isRemove, boolean isLogin){
        if(isLogin) {
            player.sendPacket(Protocol.U_MINE_WAR_MY_PIT_LIST, MineWarPb.parseMyMinePitListMsg(userMinePitList, isRemove));
        }else {
            player.sendPacket(Protocol.U_MINE_WAR_MY_PIT_SYNC, MineWarPb.parseMyMinePitListMsg(userMinePitList, isRemove));
        }
    }

    private void syncMineRefuge(List<UserMineRefuge> userMineRefugeList){
        player.sendPacket(Protocol.U_MINE_WAR_REFUGE_DATA, MineWarPb.parseMineRefugeListMsg(userMineRefugeList));
    }

    private void syncUserMineWarPatronsData(List<UserMineWarPatronsData> userMineWarPatronsDataList){
        player.sendPacket(Protocol.U_MINE_WAR_PATRONS_DATA_SYNC, MineWarPb.parseMineWarPatronsDataListMsg(userMineWarPatronsDataList));
    }

    private void syncUserMineWarPatronsData(UserMineWarPatronsData userMineWarPatronsData){
        if(null != userMineWarPatronsData){
            List<UserMineWarPatronsData> syncList = new ArrayList<>();
            syncList.add(userMineWarPatronsData);
            syncUserMineWarPatronsData(syncList);
        }
    }

    public void checkMineRefugeInit() {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.MineWar.getValue())) {
            return;
        }
        int titleId = player.getTitleId();
        List<UserMineRefuge> syncList = new ArrayList<>();
        for(Map.Entry<Integer, Integer> dataEntry : ConfigMgr.getMineOpenNeedTitleMap().entrySet()) {
            if(titleId >= dataEntry.getValue()) {//身份达到解锁条件
                if(!userMineRefugeMap.containsKey(dataEntry.getKey())){
                    userMineRefugeMap.put(dataEntry.getKey(), new ConcurrentHashMap<>());
                }
                Map<Integer, UserMineRefuge> userRefugeMap = userMineRefugeMap.get(dataEntry.getKey());
                for (int i = 0; i < GameConfig.MINE_REFUGE_PIT_COUNT; i++) {
                    if (!userRefugeMap.containsKey(i + 1)) {
                        UserMineRefuge userMineRefuge = new UserMineRefuge();
                        userMineRefuge.setMineType(dataEntry.getKey());
                        userMineRefuge.setPitId(i + 1);
                        userMineRefuge.setUserId(player.getUserId());
                        userMineRefuge.setJoinTime(0);
                        userMineRefuge.setLastGetRewardTime(0);
                        userMineRefuge.setPatronsIdList(new ArrayList<>());
                        userMineRefuge.setInsertOption();
                        userRefugeMap.put(userMineRefuge.getPitId(), userMineRefuge);
                        syncList.add(userMineRefuge);
                    }
                }
            }
        }
        if(syncList.size() > 0){
            syncMineRefuge(syncList);
        }
    }

    private void initUserMineWarPatronsData(int patronsId) {
        if(!mineWarPatronsDataMap.containsKey(patronsId) && player.getModule(PatronsModule.class).getUserPatronsMap().containsKey(patronsId)){
            UserMineWarPatronsData userMineWarPatronsData = new UserMineWarPatronsData();
            userMineWarPatronsData.setUserId(player.getUserId());
            userMineWarPatronsData.setPatronsId(patronsId);
            userMineWarPatronsData.setBattleTimes(0);
            userMineWarPatronsData.setBuyBattleTimes(0);
            userMineWarPatronsData.setDispatchMine(0);
            userMineWarPatronsData.setModifyTime(System.currentTimeMillis() / 1000);
            userMineWarPatronsData.setInsertOption();
            mineWarPatronsDataMap.put(userMineWarPatronsData.getPatronsId(), userMineWarPatronsData);
        }
    }

    /**
     * 门客是否派驻中
     */
    private boolean isPatronsDispatchMine(int patronsId){
        UserMineWarPatronsData userMineWarPatronsData = mineWarPatronsDataMap.get(patronsId);
        if(null == userMineWarPatronsData){
            return false;
        }
        return userMineWarPatronsData.getDispatchMine() > 0;
    }

    public MineWarProto.MineRefugeDispatchRespMsg.Builder refugeDispatch(int mineType, int pitId, List<Integer> patronsIdList) {
        MineWarProto.MineRefugeDispatchRespMsg.Builder msg = MineWarProto.MineRefugeDispatchRespMsg.newBuilder();
        if(player.getTitleId() < ConfigMgr.getMineOpenNeedTitle(mineType)){
            msg.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return msg;
        }
        if(patronsIdList.size() > GameConfig.MINE_WAR_BATTLE_MAX_PATRONS_COUNT){
            msg.setRet(GameErrorCode.E_MINE_WAR_PATRONS_COUNT_OVER);
            return msg;
        }
        Map<Integer, UserMineRefuge> refugeMap = userMineRefugeMap.get(mineType);
        if (null == refugeMap) {
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_TYPE_NO_FOUND);
            return msg;
        }
        UserMineRefuge userMineRefuge = refugeMap.get(pitId);
        if(null == userMineRefuge){
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_REFUGE_NO_FOUND);
            return msg;
        }

        if(null != userMineRefuge.getPatronsIdList() && userMineRefuge.getPatronsIdList().size() > 0){
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_REFUGE_HAS_DISPATCH);
            return msg;
        }

        int ret = checkPatronsHasDispatch(patronsIdList);
        if(0 != ret){
            msg.setRet(ret);
            return msg;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        userMineRefuge.setPatronsIdList(patronsIdList);
        userMineRefuge.setJoinTime(nowTime);
        userMineRefuge.setLastGetRewardTime(nowTime);
        updateMinePitPatronsState(patronsIdList, MineWarConfigMgr.DISPATCH_MINE_SELF, mineType, pitId);
        MineWarProto.MineRefugeTempMsg.Builder refugeMsg = MineWarPb.parseMineRefugeTempMsg(userMineRefuge);
        if(null != refugeMsg){
            msg.setMineRefuge(refugeMsg);
        }
        msg.setRet(0);
        return msg;
    }

    public MineWarProto.MineRefugeRecallRespMsg.Builder refugeRecall(int mineType, int pitId) {
        MineWarProto.MineRefugeRecallRespMsg.Builder msg = MineWarProto.MineRefugeRecallRespMsg.newBuilder();
        Map<Integer, UserMineRefuge> refugeMap = userMineRefugeMap.get(mineType);
        if (null == refugeMap) {
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_TYPE_NO_FOUND);
            return msg;
        }
        UserMineRefuge userMineRefuge = refugeMap.get(pitId);
        if(null == userMineRefuge){
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_REFUGE_NO_FOUND);
            return msg;
        }
        if(null == userMineRefuge.getPatronsIdList()){
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_REFUGE_NO_DISPATCH);
            return msg;
        }
        if(userMineRefuge.getPatronsIdList().size() == 0){
            msg.setRet(GameErrorCode.E_MINE_WAR_MINE_REFUGE_NO_DISPATCH);
            return msg;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        long lastGetRewardTime = userMineRefuge.getLastGetRewardTime();
        Property reward = getPitOccupyReward(userMineRefuge, nowTime, true);
        //资源入袋
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.MineWar, eLogMoneyType.MineWarRefugeRecall);
        LogMgr.addLogMineWarReward(player.getUserId(), userMineRefuge.getMineType(), userMineRefuge.getPitId(), PropertyHelper.parsePropertyToString(reward),
                nowTime - lastGetRewardTime, true, eLogMoneyType.MineWarRefugeRecall.getValue());
        //清除状态
        updateMinePitPatronsState(userMineRefuge.getPatronsIdList(), MineWarConfigMgr.DISPATCH_MINE_NONE, 0, 0);
        userMineRefuge.setPatronsIdList(new ArrayList<>());
        int goodsId = ConfigMgr.getMineOutputItemMap().get(mineType);
        msg.setReward(reward.getCountByGoodsId(goodsId).longValue());
        MineWarProto.MineRefugeTempMsg.Builder refugeMsg = MineWarPb.parseMineRefugeTempMsg(userMineRefuge);
        if(null != refugeMsg){
            msg.setMineRefuge(refugeMsg);
        }
        msg.setRet(0);
        return msg;
    }

    public Property getOccupyReward(long rewardTime, int patronsId) {
        Property occupyReward = new Property();
        for(Map.Entry<Integer, Map<Integer, UserMineRefuge>> dataEntry : userMineRefugeMap.entrySet()) {
            for (UserMineRefuge userMineRefuge : dataEntry.getValue().values()) {
                if(userMineRefuge.getPatronsIdList().size() > 0) {
                    boolean isReward = false;
                    if(0 == patronsId || (patronsId > 0 && userMineRefuge.getPatronsIdList().contains(patronsId))){
                        isReward = true;
                    }
                    if(isReward) {
                        long lastGetRewardTime = userMineRefuge.getLastGetRewardTime();
                        Property reward = getPitOccupyReward(userMineRefuge, rewardTime, true);
                        occupyReward.addProperty(reward);
                        LogMgr.addLogMineWarReward(player.getUserId(), userMineRefuge.getMineType(), userMineRefuge.getPitId(), PropertyHelper.parsePropertyToString(occupyReward),
                                rewardTime - lastGetRewardTime, true, eLogMoneyType.MineWarGetOccupyReward.getValue());
                    }
                }
            }
        }
        return occupyReward;
    }

    private Property getPitOccupyReward(UserMineRefuge userMineRefuge, long rewardTime, boolean isUpdateRewardTime) {
        Property occupyReward = new Property();
        if (null == userMineRefuge) {
            return occupyReward;
        }
        if(userMineRefuge.getPatronsIdList().size() <= 0){
            return occupyReward;
        }
        long occupyTime = (rewardTime - userMineRefuge.getLastGetRewardTime()) / 60;
        if(occupyTime > 0) {//算钱咯
            List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getUserPatronsList(userMineRefuge.getPatronsIdList());
            BigInteger mineOutput = MineWarConfigMgr.getMineOutput(player.getUserId(), userMineRefuge.getMineType(), patronsList,
                    ConfigMgr.getMineRefugeOutputAddition(userMineRefuge.getMineType())).multiply(BigDecimal.valueOf(occupyTime)).toBigInteger();
            int goodsId = ConfigMgr.getMineOutputItemMap().get(userMineRefuge.getMineType());
            if (goodsId > 0) {
                occupyReward.addProperty(goodsId, mineOutput);
            }
            if(isUpdateRewardTime){
                userMineRefuge.setLastGetRewardTime(rewardTime);
            }
        }
        return occupyReward;
    }

    private UserMineRefuge getUserMineRefuge(int mineType, int pitId){
        Map<Integer, UserMineRefuge> refugeMap = userMineRefugeMap.get(mineType);
        if (null != refugeMap) {
            return refugeMap.get(pitId);
        }
        return null;
    }

    public int getDispatchMinePitCount(){
        int count = 0;
        for(Map.Entry<Integer, Map<Integer, UserMineRefuge>> dateEntry : userMineRefugeMap.entrySet()){
            for(UserMineRefuge userMineRefuge : dateEntry.getValue().values()){
                if(userMineRefuge.getPatronsIdList().size() > 0){
                    count++;
                }
            }
        }
        List<UserMinePit> userMinePitList = MineWarMgr.getUserMinePitList(player.getUserId());
        count = count + userMinePitList.size();
        return count;
    }

    private int checkPatronsHasDispatch(List<Integer> patronsIdList) {
        for (int patronsId : patronsIdList) {
            UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
            if(null == patronsData){
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
            if(patronsData.getDispatchMine() > 0){
                return GameErrorCode.E_MINE_WAR_PATRONS_HAS_DISPATCH;
            }
        }
        return 0;
    }

    void resetOneDay(){
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
        List<UserMineWarPatronsData> syncList = new ArrayList<>();
        for(UserMineWarPatronsData userMineWarPatronsData : mineWarPatronsDataMap.values()){
            if(userMineWarPatronsData.getModifyTime() < zeroTime){
                userMineWarPatronsData.setBattleTimes(0);
                userMineWarPatronsData.setBuyBattleTimes(0);
                userMineWarPatronsData.setModifyTime(zeroTime);
                syncList.add(userMineWarPatronsData);
            }
        }
        if(syncList.size() > 0){
            syncUserMineWarPatronsData(syncList);
        }
    }

    public int checkAndCostBattleTimes(List<Integer> patronsIdList) {
        int ingots = 0;         //消耗元宝数
        List<Integer> needBuyTimesPatronsId = new ArrayList<>();
        for (int patronsId : patronsIdList) {
            UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
            if(null == patronsData){
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
            if(patronsData.getDispatchMine() > 0){
                return GameErrorCode.E_MINE_WAR_PATRONS_HAS_DISPATCH;
            }
            int maxBattleTimes = getMaxMineWarBattleTimes();
            if (patronsData.getBattleTimes() >= maxBattleTimes) {
                if(patronsData.getBattleTimes() >= maxBattleTimes + GameConfig.MINE_WAR_PATRONS_MAX_BUY_BATTLE_TIMES){
                    //超过挑战次数了
                    return GameErrorCode.E_MINE_WAR_BATTLE_TIMES_OVER;
                }else {
                    needBuyTimesPatronsId.add(patronsId);
                    ingots += GameConfig.MINE_WAR_PATRONS_BUY_BATTLE_TIMES_BASE_COST + GameConfig.MINE_WAR_PATRONS_BUY_BATTLE_TIMES_ADD_COST * patronsData.getBuyBattleTimes();
                }
            }
        }
        long nowTime = System.currentTimeMillis() / 1000;
        List<UserMineWarPatronsData> syncList = new ArrayList<>();
        if (0 == ingots) {
            for(int patronsId : patronsIdList){
                UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
                patronsData.setBattleTimes(patronsData.getBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
                syncList.add(patronsData);
            }
            for(int patronsId : needBuyTimesPatronsId){
                UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
                patronsData.setBuyBattleTimes(patronsData.getBuyBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
            }
            syncUserMineWarPatronsData(syncList);
            return 0;
        }else {
            if (!player.getModule(CurrencyModule.class).removeCurrency(new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(ingots)),
                    eLogMoneyType.MineWar, eLogMoneyType.MineWarBuyBattleTimes)) {//元宝不足
                return GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
            }
            for(int patronsId : patronsIdList){
                UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
                patronsData.setBattleTimes(patronsData.getBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
                syncList.add(patronsData);
            }
            for(int patronsId : needBuyTimesPatronsId){
                UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
                patronsData.setBuyBattleTimes(patronsData.getBuyBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
            }
            syncUserMineWarPatronsData(syncList);
            return 0;
        }
    }

    public void updateMinePitPatronsState(List<Integer> patronsIdList, int dispatchMine, int type, int pitId) {
        List<UserMineWarPatronsData> syncList = new ArrayList<>();
        for(int patronsId : patronsIdList){
            UserMineWarPatronsData patronsData = getUserMineWarPatronsData(patronsId);
            patronsData.setDispatchMine(dispatchMine);
            patronsData.setType(type);
            patronsData.setPitId(pitId);
            syncList.add(patronsData);
        }
        syncUserMineWarPatronsData(syncList);
    }

    public void notifyAddPatrons(UserPatrons userPatrons){
        if(SystemOpenMgr.systemOpen(player, eSystemId.MineWar.getValue())) {
            UserMineWarPatronsData patronsData = getUserMineWarPatronsData(userPatrons.getPatronsId());
            if (null != patronsData) {
                syncUserMineWarPatronsData(patronsData);
            }
        }
    }

    public void patronsAbilityChange(List<UserPatrons> patronsList){
        for(UserPatrons userPatrons : patronsList) {
            UserMineWarPatronsData patronsData = getUserMineWarPatronsData(userPatrons.getPatronsId());
            if(patronsData.getDispatchMine() == MineWarConfigMgr.DISPATCH_MINE_PUBLIC) {
                MineWarMgr.addTask(new PatronsAbilityChangeTask(player, patronsData.getType(), patronsData.getPitId(), userPatrons));
            }
        }
    }

    private UserMineWarPatronsData getUserMineWarPatronsData(int patronsId){
        initUserMineWarPatronsData(patronsId);
        return mineWarPatronsDataMap.get(patronsId);
    }

    public UserMineData getUserMineData() {
        return userMineData;
    }

    private int getMaxMineWarBattleTimes(){
        return GameConfig.MINE_WAR_PATRONS_DEFAULT_BATTLE_TIMES + player.getModule(CardModule.class).mineWarAddBattleTimes();
    }

    private void initUserMineData(long userId) {
        if(null == userMineData) {
            UserMineData mineData = new UserMineData();
            mineData.setUserId(userId);
            mineData.setTotalRobTimes(0);
            mineData.setTotalBeRobTimes(0);
            mineData.setMaxTeamReward1(BigInteger.ZERO);
            mineData.setMaxTeamReward2(BigInteger.ZERO);
            mineData.setMaxTeamReward3(BigInteger.ZERO);
            mineData.setTotalReward1(BigInteger.ZERO);
            mineData.setTotalReward2(BigInteger.ZERO);
            mineData.setTotalReward3(BigInteger.ZERO);
            mineData.setInsertOption();
            userMineData = mineData;
            MineWarMgr.setUserMineData(userMineData);
        }
    }
}
